package com.quicklife.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import com.quicklife.pojo.Tuser;
import com.quicklife.service.inter.CheckInService;
import com.quicklife.service.inter.FileUploadService;
import com.quicklife.util.JsonUtil;
import com.quicklife.util.SqlHelper;
import com.quicklife.util.StringUtil;

/**
 * 签到
 * 
 * @author HackerD
 * 
 */
public class CheckInServiceImpl implements CheckInService {

	@Override
	public String checkIn(String rqinfos) {

		try {
			JSONObject jsonObject = JSONObject.fromObject(rqinfos);
			String id = jsonObject.getString("userid");
			String busid = jsonObject.getString("busid");
			String mood = jsonObject.getString("mood");

			// 查询最后一次签到是什么时候
			String last = "select (sysdate-max(ac_time))*24 count from action_logs t where bussid=? and userid=? and ac_type=1";
			List list = SqlHelper.executeQuery(last,
					new String[] { busid, id }, null);
			Map map = (Map) list.get(0);
			if (map.get("count") != null
					&& ((BigDecimal) map.get("count")).doubleValue() > 0
					&& ((BigDecimal) map.get("count")).doubleValue() < 6) {
				return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
						"6小时内只能签到一次");
			}

			// 插入SQL语句
			String sql = "insert into action_logs(id,userid,bussid,ac_type,ac_time,comments,photo,video,recording) "
					+ "values(?,?,?,1,sysdate,?,?,?,?)";
			String[] params = new String[7];
			params[0] = StringUtil
					.toString(SqlHelper.getTableId("action_logs"));
			params[1] = id;
			params[2] = busid;
			params[3] = mood;

			if (jsonObject.has("photo")) {
				params[4] = jsonObject.getString("photo");
			} else {
				params[4] = "";
			}

			if (jsonObject.has("video")) {
				params[5] = jsonObject.getString("video");
			} else {
				params[5] = "";
			}

			if (jsonObject.has("recording")) {
				params[6] = jsonObject.getString("recording");
			} else {
				params[6] = "";
			}

			SqlHelper.executeUpdate(sql, params);

			// 更新我的积分
			String updateSql = "update tuser set score=score+10,checkins=checkins+1 where id=?";
			SqlHelper.executeUpdate(updateSql, new String[] { id });

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK,
					"签到成功,积分+10");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String checkInWithFile(HttpServletRequest request) {
		try {
			FileUploadService fileUploadService = new FileUploadServiceImpl();
			String tempInfos = fileUploadService.getUploadInfos(request);
			String rtinfos = checkIn(tempInfos);
			JSONObject jsonObject = JSONObject.fromObject(rtinfos);

			if (jsonObject.getString("result").equalsIgnoreCase(
					StringUtil.RESULT_CODE_OK)) {
				// 保存文件
				fileUploadService.saveFile();
				return rtinfos;
			}

			return rtinfos;
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String comment(String rqinfos) {
		// TODO 拍照、录像、录音上传问题

		try {
			// 解析Json
			JSONObject jsonObject = JSONObject.fromObject(rqinfos);
			String content = jsonObject.getString("comct");
			String userId = jsonObject.getString("userid");
			String busId = jsonObject.getString("busid");
			String score = jsonObject.getString("score");

			// 查询最后一次签到是什么时候
			String last = "select (sysdate-max(AC_TIME))*24 count from action_logs t where bussid=? and userid=? and ac_type=1";
			List list = SqlHelper.executeQuery(last, new String[] { busId,
					userId }, null);
			Map map = (Map) list.get(0);

			if (map.get("count") == null
					|| ((BigDecimal) map.get("count")).doubleValue() > 6) {
				return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
						"请先签到");
			}

			// 查询最后一次评价是什么时候
			last = "select (sysdate-max(AC_TIME))*24 count from action_logs t where bussid=? and userid=? and ac_type=3";
			list = SqlHelper.executeQuery(last, new String[] { busId, userId },
					null);
			map = (Map) list.get(0);

			if (map.get("count") != null
					&& ((BigDecimal) map.get("count")).doubleValue() < 6) {
				return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
						"评价技能尚未冷却，请稍候再试");
			}

			// 插入SQL语句
			String sql = "insert into action_logs(id,userid,bussid,ac_type,ac_time,comments,score) "
					+ "values(?,?,?,3,sysdate,?,?)";
			SqlHelper.executeUpdate(
					sql,
					new String[] {
							StringUtil.toString(SqlHelper
									.getTableId("action_logs")), userId, busId,
							content, score });

			// 更新商家积分
			String updateSql = "update business set score=(select avg(score) from action_logs where ac_type=3 and bussid=?)"
					+ "where id=?";
			SqlHelper.executeUpdate(updateSql, new String[] { busId, busId });
			// 更新我的积分
			String updateSql2 = "update tuser set score=score+10,comments=comments+1 where id=?";
			SqlHelper.executeUpdate(updateSql2, new String[] { userId });

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK,
					"评价成功,积分+10");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	@Override
	public String collect(String rqinfos) {
		try {
			// 解析Json
			JSONObject jsonObject = JSONObject.fromObject(rqinfos);
			String userId = jsonObject.getString("userid");
			String busId = jsonObject.getString("busid");

			// 查询是否已收藏
			String result = SqlHelper.checkBeExist(
					"favorite t where busid=? and userid=?", new String[] {
							busId, userId }, "已收藏");
			if (!StringUtil.isNull(result)) {
				return result;
			}

			// 插入SQL语句
			String sql = "insert into favorite(ID,USERID,BUSSINESS,ACT_TIME) values(?,?,?,sysdate)";
			SqlHelper.executeUpdate(
					sql,
					new String[] {
							StringUtil.toString(SqlHelper
									.getTableId("favorite")), userId, busId });
			// 收藏+1
			String updateSql = "update tuser set collections=collections+1 where id=?";
			SqlHelper.executeUpdate(updateSql, new String[] { userId });

			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK, "收藏成功");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

}
